<!--
 * @Descripttion: 
 * @version: 
 * @Date: 2021-04-20 11:06:21
 * @LastEditors: huzhushan@126.com
 * @LastEditTime: 2022-09-27 18:24:27
 * @Author: huzhushan@126.com
 * @HomePage: https://huzhushan.gitee.io/vue3-element-admin
 * @Github: https://github.com/huzhushan/vue3-element-admin
 * @Donate: https://huzhushan.gitee.io/vue3-element-admin/donate/
-->
<template>
  <div class="login">
    <div class="logo-box">
      <img class="logo" src="@/assets/logo.png" />
      <span class="text">中瑨联屏充电宝系统</span>
    </div>
    <el-form class="form" :model="model" :rules="rules" ref="loginForm">
      <h1 class="title">您好，欢迎登录！</h1>
      <el-form-item prop="userName">
        <el-input
          class="text"
          v-model="model.userName"
          clearable
          placeholder="手机号"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          class="text"
          v-model="model.password"
          show-password
          clearable
          placeholder="密码"
        />
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          class="text"
          v-model="model.code"
          clearable
          placeholder="验证码"
        >
          <template #suffix>
            <el-button type="primary" @click="getCodeFunc">
              {{ codeName }}
            </el-button>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          class="btn"
          size="large"
          :disabled="btnDisabled"
          @click="submit"
        >
          {{ btnText }}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
  <!-- <div class="change-lang">
    <change-lang />
  </div> -->
</template>

<script>
import {
  defineComponent,
  getCurrentInstance,
  reactive,
  toRefs,
  ref,
  computed,
  watch,
} from 'vue'
// import { Login } from '@/api/login'
import { useRouter, useRoute } from 'vue-router'
import ChangeLang from '@/layout/components/Topbar/ChangeLang.vue'
import useLang from '@/i18n/useLang'
import { useApp } from '@/pinia/modules/app'

export default defineComponent({
  components: { ChangeLang },
  name: 'login',
  setup() {
    const { proxy: ctx } = getCurrentInstance() // 可以把ctx当成vue2中的this
    const router = useRouter()
    const route = useRoute()
    const { lang } = useLang()
    watch(lang, () => {
      state.rules = getRules()
    })
    const getRules = () => ({
      userName: [
        {
          required: true,
          message: '请输入账号',
          trigger: 'blur',
        },
      ],
      password: [
        {
          required: true,
          message: '请输入密码',
          trigger: 'blur',
        },
        // {
        //   min: 6,
        //   max: 12,
        //   message: ctx.$t('login.rules-regpassword'),
        //   trigger: 'blur',
        // },
      ],
      code: [
        {
          required: true,
          message: '请输入验证码',
          trigger: 'blur',
        },
      ],
    })
    const state = reactive({
      model: {
        userName: '111',
        password: 'adminadmin',
        code: '888888',
      },
      rules: getRules(),
      loading: false,
      btnText: computed(() =>
        state.loading ? ctx.$t('login.logining') : ctx.$t('login.login')
      ),
      loginForm: ref(null),
      submit: () => {
        if (state.loading) {
          return
        }
        state.loginForm.validate(async valid => {
          if (valid) {
            state.loading = true
            router.push('/')
            // const { code, data, message } = await Login(state.model)
            // if (true) {
            //   ctx.$message.success({
            //     message: ctx.$t('login.loginsuccess'),
            //     duration: 1000,
            //   })

            //   const targetPath = decodeURIComponent(route.query.redirect)
            //   if (targetPath.startsWith('http')) {
            //     // 如果是一个url地址
            //     window.location.href = targetPath
            //   } else if (targetPath.startsWith('/')) {
            //     // 如果是内部路由地址
            //     router.push(targetPath)
            //   } else {
            //     router.push('/')
            //   }
            useApp().initToken(11111)
            // } else {
            //   // ctx.$message.error(message)
            // }
            // state.loading = false
          }
        })
      },
      codeTime: 60,
      codeName: '验证码',
      btnDisabled: false,
      getCodeFunc: () => {
        if (state.btnDisabled) return
        state.btnDisabled = true
        state.codeTime = 60
        let interval = setInterval(() => {
          console.log(state.codeTime)
          if (state.codeTime == 1) {
            state.codeName = `重新获取`
            state.btnDisabled = false
            clearInterval(interval)
            return
          }
          state.codeTime--
          state.codeName = `${state.codeTime}秒后重新获取`
        }, 1000)
      },
    })

    return {
      ...toRefs(state),
    }
  },
})
</script>

<style lang="scss" scoped>
.login {
  transition: transform 1s;
  transform: scale(1);
  width: 100%;
  height: 100%;
  overflow: hidden;
  // background: #2d3a4b;
  background-image: url('@/assets/loginBg.png');
  background-size: cover;
  position: relative;
  .logo-box {
    position: absolute;
    display: flex;
    align-items: center;
    top: 64px;
    left: 50px;
    .logo {
      width: 40px;
      height: 40px;
      margin-right: 10px;
    }
    .text {
      font-weight: bold;
      font-size: 28px;
      color: #435fc1;
    }
  }
  .form {
    width: 449px;
    height: 534px;
    background: #ffffff;
    box-shadow: 11px 14px 40px 0px rgba(15, 35, 136, 0.13);
    border-radius: 12px;
    position: absolute;
    transform: translate(-50%, -40%);
    top: 40%;
    left: 50%;
    background-color: #fff;
    padding: 55px 50px;
    box-sizing: border-box;
    :deep {
      .el-form-item {
        margin-bottom: 24px;
      }
      .el-form-item__error {
        margin-top: 6px;
      }
      //   .el-input__wrapper {
      //     // box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.1) inset;
      //     // background: rgba(0, 0, 0, 0.1);
      //   }
      //   // .el-input-group--append > .el-input__wrapper {
      //   //   border-top-right-radius: 0;
      //   //   border-bottom-right-radius: 0;
      //   // }
      //   // .el-input-group--prepend > .el-input__wrapper {
      //   //   border-top-left-radius: 0;
      //   //   border-bottom-left-radius: 0;
      //   // }
    }
    .title {
      font-weight: 600;
      font-size: 30px;
      color: #2f2f2f;
      text-align: left;
      margin-bottom: 50px;
    }
    .text {
      font-size: 16px;
      :deep(.el-input__inner) {
        // color: #fff;
        height: 48px;
        line-height: 48px;
        &::placeholder {
          // color: rgba(255, 255, 255, 0.2);
        }
      }
    }
    .btn {
      width: 100%;
      height: 50px;
      font-weight: 500;
      font-size: 16px;
      color: #ffffff;
    }
  }
}
.change-lang {
  position: fixed;
  right: 20px;
  top: 20px;
  :deep {
    .change-lang {
      height: 24px;
      &:hover {
        background: none;
      }
      .icon {
        color: #fff;
      }
    }
  }
}
</style>
